package edu.gdkm.test;

import edu.gdkm.sql.dao.*;
import edu.gdkm.sql.pojo.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class Main {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    private CategoryMapper categoryMapper;

    private ProductMapper productMapper;
    private ProductdetailMapper productdetailMapper;
    private UserinfoMapper userinfoMapper;
    private OrderdetailMapper orderdetailMapper;


    {
        String resources = "mybatis-config.xml";
        // 创建流
        Reader reader = null;
        try {
            // 读取mybatis-config.xml文件到reader对象中
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 初始化mybatis, 创建SqlSessionFactory类的对象
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        // 创建session对象
        sqlSession = sqlSessionFactory.openSession(true);
        productMapper = sqlSession.getMapper(ProductMapper.class);
        categoryMapper = sqlSession.getMapper(CategoryMapper.class);
        productdetailMapper = sqlSession.getMapper(ProductdetailMapper.class);
        userinfoMapper = sqlSession.getMapper(UserinfoMapper.class);
        orderdetailMapper=sqlSession.getMapper(OrderdetailMapper.class);

    }
    public static void main(String[] args) {

        while (true) {
            System.out.println("请输入操作：");
            System.out.println("1:查询商品");
            System.out.println("2:查询用户订单");
            System.out.println("请选择：");
            int i = new java.util.Scanner(System.in).nextInt();
            switch (i) {
                case 1:
                    System.out.println("请输入商品名：");
                    String name = new java.util.Scanner(System.in).nextLine();
                    new Main().showProduct(name);
                    break;
                case 2:
                    System.out.println("请输入用户名：");
                    String username = new java.util.Scanner(System.in).nextLine();
                    new Main().showOrders(username);
                    break;
                default:
                    System.out.println("输入错误");
            }
        }

    }
    //根据商品获得商品类型和商品详情的功能
    public void showProduct(String name){
        Object result = productMapper.findByName(name);
        if (result instanceof List) {
            List<Product> products = (List<Product>) result;
            for (Product product : products) {
                System.out.println(product);
            }
        } else if (result instanceof Product) {
            Product product = (Product) result;
            System.out.println(product);
        }

    }
    //根据用户获取订单详情功能
    public void showOrders(String username){
        Userinfo userinfo = userinfoMapper.findByUsername(username);
        System.out.println(userinfo.getOrder());

    }}
